﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.DAL;
namespace BestBuy.PageGuest
{
    public partial class PageRegister : System.Web.UI.Page
    {
        private CDanhMuc _cdm = new CDanhMuc();
        private CThongTinCaNhan _ctt = new CThongTinCaNhan();
        private CCountry _cqg = new CCountry();
        private CCongTy _cct = new CCongTy();
        private bool _flag = true;
        protected void Page_Load(object sender, EventArgs e)
        {
            lbThongBao.Visible = false;
            LoadCombobox();
        }


        private void LoadCombobox() {
            List<DanhMuc> list = _cdm.GetListCataLogIndustry();
            if (list.Count > 0) {
                ddlIndustry.DataSource = list;
                ddlIndustry.DataTextField = "TenDanhMuc";
                ddlIndustry.DataValueField = "IDDanhMuc";
                ddlIndustry.DataBind();
            }
            List<QuocGia> l = _cqg.GetList();
            if (l.Count > 0) {
                ddlCountry.DataSource = l;
                ddlCountry.DataTextField = "TenQuocGia";
                ddlCountry.DataValueField = "IDQuocGia";
                ddlCountry.DataBind();
            }
        }

        protected void btnRe_Click(object sender, EventArgs e)
        {
            Response.Redirect("PageRegister.aspx");
        }

        protected void cusvEmail_ServerValidate(object source, ServerValidateEventArgs args)
        {
            string email = txtEmail.Text;
            bool f = _ctt.EmailExistTest(email);
            if (f)
            {
                args.IsValid = true;
            }
            else
            {
                _flag = false;
                args.IsValid = false;
            }
        }

        protected void btnJoin_Click(object sender, EventArgs e)
        {
            if (_flag)
            {
                lbThongBao.Visible = true;
                ThongTinTaiKhoan tttk = new ThongTinTaiKhoan();
                CongTy ct = new CongTy();
                tttk.IDQuocGia = Convert.ToInt32(ddlCountry.SelectedValue);
                tttk.Email = txtEmail.Text;
                tttk.DienThoai = txtPhone.Text;
                tttk.HoTen = txtFirstname.Text + " " + txtLastname.Text;
                tttk.TinhTrang = true;
                if (!txtYahoo.Text.Equals(""))
                {
                    tttk.Yahoo = txtYahoo.Text;
                }
                if (!txtSkype.Text.Equals(""))
                {
                    tttk.Skype = txtSkype.Text;
                }
                if (rbtType.SelectedValue != "-1") {
                    tttk.LoaiND = Convert.ToInt32(rbtType.SelectedValue);
                    if (tttk.LoaiND == 1)
                    {
                        tttk.SanPhamMua = txtProduct.Text;
                    }
                    else {
                        tttk.SanPhamBan = txtProduct.Text;
                    }
                }
                if (rblGender.SelectedValue != "-1")
                {
                    tttk.GioiTinh = Convert.ToBoolean(rblGender.SelectedValue);
                }
                tttk.MatKhau = _ctt.MaHoaMatKhau(txtPass.Text);
                ct.TenCongTy = txtCompany.Text;
                ct.IDNganh = Convert.ToInt32(ddlIndustry.SelectedValue);
                int idcongty = _cct.InsertCompany(ct);
                if (idcongty > 0)
                {
                    bool kq = _ctt.InsertInfoUser(tttk);
                    if (kq)
                    {
                        Response.Write(@"<script type='text/javascript'>alert('You registered success.');window.location = '/';</script>");
                    }
                    else
                    {
                        pnThongBao.CssClass = "InLeft error message";
                        lbThongBao.Text = "Registered fail.";

                    }
                }
                else
                {
                    pnThongBao.CssClass = "InLeft error message";
                    lbThongBao.Text = "Registered fail.";
                }
            }
        }
    }
}